﻿using CustomCharacter;
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;
using Exiled.Events.EventArgs.Player;
using MEC;
using PlayerRoles;
using System.Collections.Generic;
using System.ComponentModel;
using PlayerStatsSystem;
using UnityEngine;
using VoiceChat;
using CustomCharacter.Ability;
using CustomCharacter.Interfaces;
using RgCore.Events;

namespace Scp3035
{
    [RoleInfo("SCP-3035", Exiled.API.Enums.Side.Scp, PlayerRoles.RoleTypeId.ClassD, PlayerRoles.RoleTypeId.ClassD)]
    public class Scp3035 : CustomCharacter.CustomCharacter, IScpNumber, IRoundStartSpawn, ITrialCard, IVoiceChannelModifcation, IBadgeVisibilityModified
    {
        public Scp3035(int id) : base(id)
        {
        }

        public override Color32 RoleColor => new Color32(198, 226, 255, 255);

        public int Cost => 450;
        public string ScpNumber => "3035";

        public byte MinPlayers => 10;
        public VoiceChatChannel[] AvailableVoiceChannels => new VoiceChatChannel[] { VoiceChatChannel.ScpChat};
        [Description("血量积累")]
        public int AccumulatedHealth
        {
            get
            {
                var attr = Player.GetAbility("Scp3035_Attribute_1");
                if (attr.Ability == null)
                    return -1;
                if (attr.Datas["transformed"] is bool b && b)
                    return -1;
                return (int)attr.Datas["health"];
            }
        }

        public bool PrimaryModification => true;
        public string[] SideIdList => new string[] { Side.Scp.ToString() };

        public bool IsBlacklist => false;

        public static bool Ability1_Checker(Player player, out string response)
        {
            if (Round.ElapsedTime.TotalMinutes < 3)
            {
                response = "三分钟之前你无法主动变身";
                return false;
            }
            var attr = player.GetAbility("Scp3035_Attribute_1");
            if (attr.Ability == null)
            {
                response = "此技能需要SCP-3035的特性技能";
                return false;
            }
            if (attr.Datas["transformed"] is bool b && b)
            {
                response = "你已经变身了";
                return false;
            }
            response = "";
            return true;
        }
        public static AbilityOutput Ability1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            RoleTypeId[] scp = new RoleTypeId[]
{
                RoleTypeId.Scp049, RoleTypeId.Scp096, RoleTypeId.Scp3114, RoleTypeId.Scp173, RoleTypeId.Scp106, RoleTypeId.Scp939
};
            var attr = player.GetAbility("Scp3035_Attribute_1");
            attr.Datas["transformed"] = true;
            int health = (int)attr.Datas["health"];
            player.Role.Set(scp[Random.Range(0, scp.Length)], RoleSpawnFlags.None);
            Timing.CallDelayed(1f, delegate ()
             {
                 player.MaxHealth = health;
                 player.Health = health;
            });
            return new AbilityOutput(true);
        }

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new Ability("Scp3035_Offensive_1", "立即随机变为一个SCP（开局前3分钟不允许自行触发）", AbilityType.Offensive, 0, 
                Ability1, Ability1_Checker, null, null, GetType()),
                new AttributeAbility("Scp3035_Attribute_1", "存活或拾取物品来吞噬它并获得血量积累，死亡或使用1技能后随机变为一种SCP", Attribute_1,
               true, new IExiledEvent[] { Exiled.Events.Handlers.Player.PickingUpItem, EventProcessor.OnDying}, new KeyValuePair<string, object>[]
               {
                   new KeyValuePair<string, object>("health", 500),
                   new KeyValuePair<string, object>("transformed", false)
               }, GetType())
            };
        }

        private static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (trigger == AbilityTrigger.Update)
            {
                if (info.Datas["transformed"] is bool b && !b)
                {
                    info.Datas["health"] = (int)info.Datas["health"] + 1;
                }
                return true;
            }
            if (trigger != AbilityTrigger.Event)
            {
                return new AbilityOutput(false);
            }
            if (e is PickingUpItemEventArgs ev3 && ev3.Player == player)
            {
                var attr = player.GetAbility("Scp3035_Attribute_1");
                if (attr.Ability != null && attr.Datas["transformed"] is bool b && !b)
                {
                    ev3.IsAllowed = false;
                    int adds = (int)(ev3.Pickup.Weight * 10);
                    ev3.Pickup.Destroy();
                    info.Datas["health"] = (int)info.Datas["health"] + adds;
                    return true;
                }
            }
            if (e is DyingEventArgs ev4 && ev4.Player == player && info.Datas["transformed"] is bool x && !x)
            {
                if (ev4.DamageHandler.Base is WarheadDamageHandler || (ev4.DamageHandler.Base is UniversalDamageHandler && ev4.Player.TryGetEffect(EffectType.Decontaminating,out _)))
                {
                    return new AbilityOutput(false);
                }
                ev4.IsAllowed = false;
                RoleTypeId[] scp = new RoleTypeId[]
{
                RoleTypeId.Scp049, RoleTypeId.Scp096, RoleTypeId.Scp3114, RoleTypeId.Scp173, RoleTypeId.Scp106, RoleTypeId.Scp939
};
                info.Datas["transformed"] = true;
                int health = (int)info.Datas["health"];
                player.Role.Set(scp[Random.Range(0, scp.Length)], RoleSpawnFlags.None);
                Timing.CallDelayed(1f, delegate ()
                {
                    player.MaxHealth = health;
                    player.Health = health;
                });
                return true;
            }
            return new AbilityOutput(false);
        }
    }
}